package com.controller.oos;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.service.oos.OosTaskService;
import com.model.oos.OosTaskModel;

/**
 *
 */
@Controller("oosTaskController")
@RequestMapping(value = "/api")
public class OosTaskController {
    static final Logger logger = LoggerFactory.getLogger(OosTaskController.class);

    @Autowired
    OosTaskService oosTaskService;

    /**
     * url映射
     *
     * @param map
     * @return
     */
    @RequestMapping("qm/oos/task")
    public Map<String, String> oosTask() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("key1", "value-1");
        return map;
    }

    /**
     * 查询列表
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "qm/oos/task/selectList", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult selectList(@RequestParam Map<String, Object> map) {
        return oosTaskService.selectList(map);
    }

    /**
     * 查询详情
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "qm/oos/task/selectInfor", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult selectInfor(@RequestParam String taskId) {
        return oosTaskService.selectInfor(taskId);
    }

    /**
     * 保存询价
     *
     * @param
     * @return
     */
    @RequestMapping(value = "qm/oos/task/save", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult save(@RequestBody OosTaskModel oosTaskModel) {
        return oosTaskService.save(oosTaskModel);
    }

    /**
     * 退回，status =0
     *
     * @param
     * @return
     */
    @RequestMapping(value = "qm/oos/task/backUp", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult backUp(@RequestParam String taskId) throws IOException {
        return oosTaskService.backUp(taskId) > 0 ? HttpResult.ok("已退回") : HttpResult.error("退回失败");
    }

    /**
     * 删除询价
     *
     * @param
     * @return
     */
    @RequestMapping(value = "qm/oos/task/delete", method = {RequestMethod.DELETE})
    @ResponseBody
    public HttpResult delete(@RequestParam String taskId) {
        return oosTaskService.delete(taskId);
    }

    /**
     * 导出
     *
     * @param map
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "qm/oos/task/export")
    public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        oosTaskService.export(map, response);

    }
}
